package net.oschina.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;

import net.oschina.app.adapter.FriendsAdapter;
import net.oschina.app.base.BaseAdapter;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.base.BaseProtocol;
import net.oschina.app.bean.ActiveBean;
import net.oschina.app.bean.ExploreFriendBean;
import net.oschina.app.interf.FragmentType;
import net.oschina.app.ui.DetailFragmentActivity;
import net.oschina.app.util.CommonUtils;

import java.util.List;

import retrofit2.Call;

/**
 * Created by wei03 on 2017/1/17.
 */

public class ExploreFriendsFragment extends BaseFragment {

    private FriendsAdapter mFriendsAdapter;
    private ExploreFriendBean mFriendBean;
    private List<ActiveBean> mActiveBeen;

    @Override
    protected BaseAdapter getRvAdapter() {
        mFriendsAdapter = new FriendsAdapter(getContext());
        return mFriendsAdapter;
    }

    @Override
    protected void OnSubItemClick(RecyclerAdapterWithHF adapter, RecyclerView.ViewHolder vh, int position) {
        Intent intent = new Intent(CommonUtils.getContext(), DetailFragmentActivity.class);
        intent.putExtra(FragmentType.FRAGMENT_TYPE, FragmentType.TYPE_FRIENDS_DETAIL);
        ActiveBean activeBean = mFriendBean.getActivies().get(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("bean", activeBean);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    protected int onLoadSubData() {
        BaseProtocol<ExploreFriendBean> baseProtocol = new BaseProtocol<ExploreFriendBean>() {
            @Override
            protected Call<ExploreFriendBean> getCall(String pageIndex) {
                return mApiService.getExploreFriendBean("3226664", "0", "1", "20");
            }
        };
        baseProtocol.setPageIndex(mPageIndex);
        mFriendBean = baseProtocol.getData(mList.size() /20);
        if (mFriendBean != null) {
            mPageIndex++;
            mActiveBeen = mFriendBean.getActivies();
            if (mActiveBeen.size() == 0 && mPageIndex == 0) {            //没有数据
                return STATE_EMPTY;
            }
            mList.addAll(mFriendBean.getActivies());
            CommonUtils.runOnMainThread(new Runnable() {
                @Override
                public void run() {
                    if (mActiveBeen.size() == 0) {
                        Toast.makeText(getContext(), "没有更多数据", Toast.LENGTH_SHORT).show();
                        ptrClassicFrameLayout.setLoadMoreEnable(false);
                    }
                    mFriendsAdapter.notifyDataSetChanged();
                }
            });
            return STATE_SUCCESS;
        }
        return STATE_EMPTY;
    }
}
